<template>
  <vxe-modal @close="cancel" title="选择物资" v-model="wzDuoX" width="80%" showFooter show-zoom resize>
    <div style="height: 70vh; overflow-y: auto;">
      <!-- 顶部搜索 -->
      <el-form :model="queryParams" :inline="true" ref="queryForm" size="small" v-show="showSearch" label-width="90px">
        <el-form-item label="物资名称" prop="wzName">
          <el-input
            v-model="queryParams.wzName"
            placeholder="请输入物资名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="物资规格" prop="wzGg">
          <el-input
            v-model="queryParams.wzGg"
            placeholder="请输入物资规格"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="物资分类" prop="wzFl">
          <el-input
            v-model="queryParams.wzFl"
            placeholder="请输入物资分类"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="物资专业" prop="wzZy">
          <el-input
            v-model="queryParams.wzZy"
            placeholder="请输入物资专业"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格 -->
      <el-table :row-key="(row)=>{ return row.wzxxId}" :reserve-selection="true"
                height="55vh" @row-click="clickRow" ref="table" highlight-current-row
                :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
                v-loading="loading" :data="ejkwzList" @selection-change="handleSelectionChange">
        <el-table-column :reserve-selection="true" type="selection" width="55" align="center"/>
        <el-table-column label="序号" align="center" type="index" :index="indexMethod"/>
        <el-table-column label="工区" align="center" prop="gq"/>
        <el-table-column label="物资名称" align="center" prop="wzName"/>
        <el-table-column label="物资单位" align="center" prop="wzDw"/>
        <el-table-column label="物资规格" align="center" prop="wzGg"/>
        <el-table-column label="物资分类" align="center" prop="wzFl"/>
        <el-table-column label="物资专业" align="center" prop="wzZy"/>
        <el-table-column label="当前库存" align="center" prop="kc"/>
      </el-table>

      <!-- 分页组件 -->
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <div slot="footer" class="dialog-footer dialog-footer-center">
        <el-button size="small" @click="submitForm">
          <svg-icon icon-class="confirm1" class-name='custom-class'/>
          确 定
        </el-button>
        <el-button size="small" @click="cancel">
          <svg-icon icon-class="cancel1" class-name='custom-class'/>
          取 消
        </el-button>
      </div>

    </div>
  </vxe-modal>
</template>

<script>
import {listEjkwz} from "@/api/ejkgl/ejkwz";

export default {
  name: "CgyWz", //昌公堰工区物资
  inject: ['updateCgyWzLabel'],
  data() {
    return {
      // 二级库物资表格数据
      ejkwzList: [],
      wzDuoX: false, //弹窗是否打开
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ejkwzId: null,
        gq: '昌公堰工区',
        wzxxId: null,
        idWhere: null,
        userId: null,
        deptId: null,
        wzName: null,
        wzGg: null,
        wzFl: null,
        wzZy: null,
      },
      // 显示搜索条件
      showSearch: true,
      wzLable: [],
      // 总条数
      total: 0,
      xzWzList: [], //选择的物资列表

    }
  },
  created() {
    this.getList();
  },
  methods: {
    handleOpen(data) {
      this.queryParams.pageNum = 1
      // 接收来自父组件的数据
      this.wzLable = data
      this.wzDuoX = true;
      this.getList();
    },
    /** 查询二级库物资列表 */
    getList() {
      this.loading = true;
      listEjkwz(this.queryParams).then(response => {
        this.ejkwzList = response.rows;
        this.total = response.total;
        this.loading = false;

        // 默认选中已有的数据
        this.$nextTick(() => {
          this.wzLable.forEach(item => {
            const selectedRow = this.ejkwzList.find(row => row.wzxxId == item.wzxxId)
            if (selectedRow) {
              this.$refs.table.toggleRowSelection(selectedRow, true);
            }
          });
        });

      });
    },
    submitForm() {
      this.updateCgyWzLabel(this.xzWzList)
      this.wzDuoX = false;
      this.$refs.table.clearSelection();
    },
    cancel() {
      this.wzDuoX = false;
      this.$refs.table.clearSelection();
    },
    //点击行 获取行
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      table.toggleRowSelection(row);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.wzxxId)
      this.single = selection.length !== 1;
      this.multiple = !selection.length;

      // 同时更新 xzWzList
      this.xzWzList = selection.map(item => ({
        wzxxId: item.wzxxId,
        wzName: item.wzName,
        wzDw: item.wzDw,
        wzGg: item.wzGg,
        wzFl: item.wzFl,
        wzZy: item.wzZy,
        ejkwzId: item.ejkwzId,
      }));
    },
    /** 自定义序号 */
    indexMethod(index) {
      let pageNum = this.queryParams.pageNum - 1;
      if ((pageNum !== -1 && pageNum !== 0)) {
        return (index + 1) + (pageNum * this.queryParams.pageSize);
      } else {
        return (index + 1)
      }
    },

  }
}
</script>

<style scoped>

</style>
